import { createRouter, createWebHistory } from 'vue-router'
import { useStore } from '@/stroe'

const routes = [
  {
    path: '/',
    name: 'index',
    component: () => import('@/views/index/Index.vue'),
  },
]

const router = createRouter({
  history: createWebHistory(),

  routes,
})
// // 路由守卫
router.beforeEach(async (to, from, next) => {
  if (to.meta.auth) {
    if (useStore().userdata === null) {
      Message.error('请登录后操作')
      next({ path: '/' })
    } else {
      if (!to.name) {
        next({ path: '/404' })
      } else {
        next()
      }
    }
  } else if (!to.name) {
    next({ path: '/404' })
  } else {
    next()
  }
})

export default router
